package starComponent.elements;


/**
 * @author Charles Perin
 * 11 avr. 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 */
public class LadderTick {

	protected double value;
	protected String label;

	protected int xCenter, yCenter, xLabel, yLabel, xCenterLabel, yCenterLabel;

	public static final int RADIUS = 5;
	protected static final int MARGIN_LABEL = 10;

	protected A_StarBranch branch;

	public LadderTick(A_StarBranch branch, double value, String label){
		this.branch = branch;
		setValue(value);
		setLabel(label);
		calculateTickCenter();
		calculateLabelPosition();
	}
	
	@Override
	public String toString(){
		return "val="+value+", label="+label+", center=("+xCenter+","+yCenter+"), label=("+xLabel+","+yLabel+")";
	}

	protected void calculateTickCenter(){
		if(branch.getLadder().getMode()==Ladder.LINEAR){
			xCenter = (int) (value*branch.getLength()*Math.cos(branch.getAngle())/branch.getMax()+branch.getxStart());
			yCenter = (int) (-value*branch.getLength()*Math.sin(branch.getAngle())/branch.getMax()+branch.getyStart());
		}
		else if(branch.getLadder().getMode()==Ladder.LOGARITHMIC){
			xCenter = (int) (Math.log10(value)*branch.getLength()*Math.cos(branch.getAngle())/Math.log10(branch.getMax())+branch.getxStart());
			yCenter = (int) (-Math.log10(value)*branch.getLength()*Math.sin(branch.getAngle())/Math.log10(branch.getMax())+branch.getyStart());
		}
	}

	private void calculateLabelPosition(){
		double angle = branch.getAngle();
		if(angle>=0){
			if(angle>15*Math.PI/8 || angle <Math.PI/8){
				xLabel = xCenter+MARGIN_LABEL/2;
				yLabel = yCenter+MARGIN_LABEL+StarComponent.LABEL_HEIGHT;
			}
			else if(angle<=Math.PI/4){
				xLabel = xCenter+MARGIN_LABEL;
				yLabel = yCenter+MARGIN_LABEL;
			}
			else if(angle<=2*Math.PI/4){
				xLabel = xCenter+MARGIN_LABEL;
				yLabel = yCenter+StarComponent.LABEL_HEIGHT/2;
			}
			else if(angle<=3*Math.PI/4){
				xLabel = xCenter;
				yLabel = yCenter-MARGIN_LABEL;
			}
			else if(angle<=4*Math.PI/4){
				xLabel = xCenter-MARGIN_LABEL;
				yLabel = yCenter-MARGIN_LABEL;
			}
			else if(angle<=5*Math.PI/4){
				xLabel = xCenter - label.length()*5-MARGIN_LABEL;
				yLabel = yCenter-StarComponent.LABEL_HEIGHT;
			}
			else if(angle<=6*Math.PI/4){
				xLabel = xCenter - label.length()*5-MARGIN_LABEL;
				yLabel = yCenter+StarComponent.LABEL_HEIGHT/2;
			}
			else if(angle<=7*Math.PI/4){
				xLabel = xCenter - label.length()*5-MARGIN_LABEL;
				yLabel = yCenter + MARGIN_LABEL+StarComponent.LABEL_HEIGHT/2;
			}
			else if(angle<=8*Math.PI/4){
				xLabel = xCenter - label.length()*5 ;
				yLabel = yCenter + MARGIN_LABEL+StarComponent.LABEL_HEIGHT;
			}
			else System.err.println("l'angle doit etre inferieur a 2PI");
			
			xCenterLabel = xLabel + label.length()*5;
			yCenterLabel = yLabel + StarComponent.LABEL_HEIGHT/2;
		}
		else System.err.println("l'angle doit etre superieur a 0");
	}


	public double getValue() {
		if(branch.getLadder().getMode()==Ladder.LINEAR){
			return value;
		}
		else if(branch.getLadder().getMode()==Ladder.LOGARITHMIC){
			return Math.log10(value);
		}
		return value;
	}

	public void setValue(double value) {
		this.value = value;
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public int getxCenter() {
		return xCenter;
	}

	public void setxCenter(int xCenter) {
		this.xCenter = xCenter;
	}

	public int getyCenter() {
		return yCenter;
	}

	public void setyCenter(int yCenter) {
		this.yCenter = yCenter;
	}

	public int getxLabel() {
		return xLabel;
	}

	public void setxLabel(int xLabel) {
		this.xLabel = xLabel;
	}

	public int getyLabel() {
		return yLabel;
	}

	public void setyLabel(int yLabel) {
		this.yLabel = yLabel;
	}


	public void setxCenterLabel(int xCenterLabel) {
		this.xCenterLabel = xCenterLabel;
	}

	public int getxCenterLabel() {
		return xCenterLabel;
	}

	public void setyCenterLabel(int yCenterLabel) {
		this.yCenterLabel = yCenterLabel;
	}

	public int getyCenterLabel() {
		return yCenterLabel;
	}
}
